<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="支付时间" prop="payFinishTime">
        <el-date-picker
          v-model="queryParams.payFinishTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="business">
        <el-select v-model="queryParams.business" placeholder="请选择" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_ORDER_BUSINESS_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分账角色" prop="role">
        <el-select v-model="queryParams.role" placeholder="请选择" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_DIVIDE_ROLE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="区域" prop="agentCode">
        <el-select v-model="queryParams.agentCode" placeholder="请选择" clearable>
          <el-option
            v-for="agent in agentList"
            :key="agent.code"
            :label="agent.name"
            :value="agent.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="storeCode">
        <el-select v-model="queryParams.storeCode" placeholder="请选择" clearable>
          <el-option
            v-for="store in storeList"
            :key="store.code"
            :label="store.name"
            :value="store.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['meijia:divide:query']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="子订单号" align="center" prop="subCode" />
      <el-table-column label="所属区域" align="center" prop="serialCode">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).agentName }}
        </template>
      </el-table-column>
      <el-table-column label="所属门店" align="center" prop="orderCode">
        <template #default="scope">
          {{ scope.row.storeName }}
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="business">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_ORDER_BUSINESS_TYPE" :value="scope.row.business" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单类型" align="center" prop="status" /> -->
      <el-table-column label="收款人" align="center" prop="remark">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).username }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="remark">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).phone }}
        </template>
      </el-table-column>
      <el-table-column label="分账角色" align="center" prop="remark">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).roleName }}
        </template>
      </el-table-column>
      <el-table-column label="收款金额" align="center" prop="remark">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).moneyCNY }}
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="remark">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).projectName }}
        </template>
      </el-table-column>
      <el-table-column label="分成比例（%）" align="center" prop="remark">
        <template #default="scope">
          {{ JSON.parse(scope.row.remark).proportion * 100 }}
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payFinishTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="分账时间" align="center" :formatter="dateFormatter" width="200px">
        <template #default="scope">
          {{ formatDate(scope.row.divideTimeBegin, 'YYYY/MM/DD') }}-{{
            formatDate(scope.row.divideTimeEnd, 'YYYY/MM/DD')
          }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as DivideApi from '@/api/meijia/divide'
import * as OrderApi from '@/api/meijia/order'
import * as AgentApi from '@/api/meijia/agent'
import * as StoreApi from '@/api/meijia/store'
import { formatDate } from '@/utils/formatTime'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
defineOptions({ name: 'Divide' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const agentList = ref([])
const getAgentList = async () => {
  agentList.value = await AgentApi.getByAgentCode({})
}
const storeList = ref([])
const getStoreList = async () => {
  storeList.value = await StoreApi.getByAgentCodeContainNull({})
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DivideApi.getDividePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DivideApi.exportDivide(queryParams)
    download.excel(data, '总分账明细.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
/** 初始化 **/
onMounted(() => {
  getStoreList()
  getAgentList()
  getList()
})
</script>
